<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ajaxUpload extends MY_Admin
{
    
    private $cfg=array();
    private $filepath;
    
    function __construct()
    {
        parent :: __construct();
        
        $this->filepath = 'uploads/'.date('Ymd').'/';
        if(!is_dir('./'.$this->filepath)) mkdir('./'.$this->filepath);
        $this->cfg['upload_path'] = './'.$this->filepath;
        $this->cfg['allowed_types'] = 'gif|jpg|png';
        //$this->cfg['max_size'] = '1000';
        $this->cfg['max_width']  = '1024';
        $this->cfg['max_height']  = '768';
        $this->cfg['encrypt_name']  = true;
    }
    
    public function uploadPicture()
    {
        if (isset($_POST["PHPSESSID"])) {
            session_id($_POST["PHPSESSID"]);
        }
        $this->load->library('upload', $this->cfg);
        $filename='Filedata';
        if (! $this->upload->do_upload($filename))
        {
            @unlink($_FILES[$filename]['tmp_name']);
            $return['message'] = $this->upload->display_errors();
            $return['code']    = '0';
        }
        else
        {
            $this->db->insert('files', $this->upload->data());
            $return['picurl'] = base_url().$this->filepath.$this->upload->file_name;
            $return['code'] = '1';
            $return['photo_id'] = $this->db->insert_id();
        }
        echo json_encode($return);
    }
}